package com.doucat.support.component.fragment.host;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.doucat.support.component.fragment.base.BaseFragment;
import com.doucat.support.component.fragment.Refreshable;
import com.doucat.support.model.inter.InterfaceView;
import com.tonly.doucatlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: HostFragment.java
 * Author: wangfy
 * Create: 2021/7/1 15:08
 *
 * 宿主Fragment，用来装载其他的Fragment
 */
public abstract class HostFragment extends BaseFragment implements InterfaceView, Refreshable {
    public List<Fragment> fragments;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments=new ArrayList<>();
    }
    private SwipeRefreshLayout refreshSummarize;
    private LinearLayout lvHostFragment;
    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setBaseFragmentContentView(R.layout.fragment_host);
        refreshSummarize=findViewById(R.id.refreshSummarize);
        refreshSummarize.setEnabled(false);
        refreshSummarize.setColorSchemeResources(R.color.white);
        refreshSummarize.setProgressBackgroundColorSchemeResource(R.color.yellow);
        refreshSummarize.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshSummarize.setRefreshing(false);
                for(int i = 0; i< fragments.size(); i++){
                    BaseFragment fragment = (BaseFragment) fragments.get(i);
                    fragment.requestRefresh();
                }
            }
        });
        lvHostFragment=findViewById(R.id.lvHostFragment);
        loadFragment();
        onRefresh();
    }

    public void activeSwipeRefreshLayout(){
        refreshSummarize.setEnabled(true);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        initContainer(lvHostFragment);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeAllChildFragment();
    }

    /**************************Fragment**************************************/

    public abstract void loadFragment();

    public void add(Fragment fragment){
        fragments.add(fragment);
    }

    private void initContainer(ViewGroup lvFragment){
        for(int i = 0; i< fragments.size(); i++){
            Fragment fragment = fragments.get(i);
            FrameLayout frameLayout=new FrameLayout(getContext());
            int ids= View.generateViewId();
            frameLayout.setId(ids);

            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            if(i!=0)layoutParams.topMargin=getResources().getDimensionPixelOffset(R.dimen.x10);
            if(i==fragments.size()-1)layoutParams.bottomMargin=getResources().getDimensionPixelOffset(R.dimen.x10);
            frameLayout.setLayoutParams(layoutParams);
            lvFragment.addView(frameLayout);
            replaceFragment(ids, fragment);
        }
    }

    public void removeAllChildFragment(){
        for(int i = 0; i< fragments.size(); i++){
            Fragment fragment = fragments.get(i);
            removeFragment(fragment);
        }
    }
}
